package com.jewelry.crm.model.dao;

import static com.jewelry.crm.common.Common.logger;
import com.jewelry.crm.common.ModelValidationException;
import com.jewelry.crm.model.domain.User;
import org.hibernate.Session;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static com.jewelry.crm.model.dao.HibernateUtil.getSessionFactory;
import static com.jewelry.crm.model.dao.HibernateUtil.getValidator;
import static com.jewelry.crm.model.dao.HibernateUtil.validate;

public class UserDaoImpl implements UserDao{

    public void addUser(User user) throws SQLException {
        try {
            if (validate(user, getValidator())) {
                if (user.getUser_role() == null) user.setUser_role("ROLE_USER");
                Session session = getSessionFactory().openSession();
                session.beginTransaction();
                session.save(user);
                session.getTransaction().commit();
                session.close();
            } else throw new ModelValidationException("User wasn't added due to validation errors");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public void updateUser(User user) throws SQLException {
        try {
            if (validate(user, getValidator())) {
                if (user.getUser_role() == null) user.setUser_role("ROLE_USER");
                Session session = getSessionFactory().openSession();
                session.beginTransaction();
                session.update(user);
                session.getTransaction().commit();
                session.close();
            } else throw new ModelValidationException("User wasn't updated due to validation errors");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public User getUserById(Long user_id) throws SQLException {
        try {
            Session session = getSessionFactory().openSession();
            User user = (User) session.get(User.class, user_id);
            session.close();
            return user;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new User();
        }
    }

    public Collection getAllUsers() throws SQLException {
        try {
            Session session = getSessionFactory().openSession();
            List users = session.createCriteria(User.class).list();
            session.close();
            return users;
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            return new ArrayList();
        }
    }

    public void deleteUser(User user) throws SQLException {
        try {
            Session session = getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(user);
            session.getTransaction().commit();
            session.close();
        }
        catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}
